package ynu.edu.elmspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.edu.elmspringboot.po.Business;
import ynu.edu.elmspringboot.service.BusinessService;
import ynu.edu.elmspringboot.utils.PageResult;

import java.util.List;

@RestController
@RequestMapping("/BusinessController")
@Api(tags = "商家信息管理")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @ApiOperation("根据订单类型id查询商家信息")
    @PostMapping("/listBusinessByOrderTypeId")
    public List<Business> listBusinessByOrderTypeId(Business business) {
        return businessService.list(new QueryWrapper<Business>().eq("order_type_id", business.getOrderTypeId()));
    }

    @ApiOperation("根据商家id查询商家信息")
    @PostMapping("/getBusinessById")
    public Business getBusinessById(Integer businessId) {
        return businessService.getById(businessId);
    }

    @ApiOperation("分页查询商家列表")
    @GetMapping("/getBusinessPage")
    public PageResult<Business> getBusinessPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("商家名称") @RequestParam(required = false) String businessName,
            @ApiParam("商家类型ID") @RequestParam(required = false) Integer orderTypeId) {
        
        Page<Business> page = new Page<>(current, size);
        IPage<Business> result = businessService.getBusinessPage(page, businessName, orderTypeId);
        return PageResult.fromIPage(result);
    }

    @ApiOperation("根据商家类型分页查询商家")
    @GetMapping("/getBusinessPageByOrderType")
    public PageResult<Business> getBusinessPageByOrderType(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("商家类型ID") @RequestParam Integer orderTypeId) {
        
        Page<Business> page = new Page<>(current, size);
        IPage<Business> result = businessService.getBusinessPageByOrderType(page, orderTypeId);
        return PageResult.fromIPage(result);
    }
}